package com.itheima.app.manager;

import com.itheima.domain.mongo.Video;
import com.itheima.domain.mongo.VideoScore;
import com.itheima.service.mongo.VideoService;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MQVideoManager {
    @Autowired(required =  false)
    private VideoService videoService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //针对视频的操作
    public static final Integer VIDEO_PUBLISH = 1; // 发布视频
    public static final Integer VIDEO_BROWSE = 2; //浏览视频
    public static final Integer VIDEO_LIKE = 3; //点赞视频
    public static final Integer VIDEO_LOVE = 4; //喜欢视频
    public static final Integer VIDEO_COMMENT = 5; //评论视频
    public static final Integer VIDEO_DISLIKE = 6; //取消点赞
    public static final Integer VIDEO_DISLOVE = 7; //取消喜欢
    public static final Integer VIDEO_FOCUS = 8; //关注用户
    public static final Integer VIDEO_UNFOCUS = 9; //取消关注

    /**
     * 针对视频的操作给消息队列发送VideoScore,为大数据推荐提供数据支持
     * @param userId
     * @param publishId
     * @param type
     */
    public void sendVideo(Long userId, ObjectId publishId, Integer type){
        VideoScore videoScore = new VideoScore();

        //根据视频id查询视频以获取vid
        Video video = videoService.findById(publishId);

        //补充信息
        videoScore.setUserId(userId);
        videoScore.setDate(System.currentTimeMillis());
        videoScore.setVideoId(video.getVid());

        //调用方法进行分数赋值
        setVideoScore(videoScore,type);

        //给消息队列发送消息
        rabbitTemplate.convertAndSend("tanhua.recommend.video",videoScore);
    }


    private void setVideoScore(VideoScore videoScore, Integer type) {
        switch (type) {
            case 1: {
                videoScore.setScore(20d);
                break;
            }
            case 2: {
                videoScore.setScore(1d);
                break;
            }
            case 3: {
                videoScore.setScore(5d);
                break;
            }
            case 4: {
                videoScore.setScore(8d);
                break;
            }
            case 5: {
                videoScore.setScore(10d);
                break;
            }
            case 6: {
                videoScore.setScore(-5d);
                break;
            }
            case 7: {
                videoScore.setScore(-8d);
                break;
            }
            case 8: {
                videoScore.setScore(15d);
                break;
            }
            case 9: {
                videoScore.setScore(-15d);
                break;
            }
        }
    }
}
